Skip to content

Conversation

JLedel
Copy link
Contributor

@JLedel JLedel commented Jul 15, 2025

…pended to the default value

…efault value is appended to the default value
Copy link
Collaborator

@DiscoPYF DiscoPYF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @JLedel for another great contribution!

Consumers may be relying on the existing behavior, so it would be good to expose a serialization option to control the behavior (ApiClientSerializationOptions).
What about keeping the default as "auto" but making it configurable?

var options = new ApiClientSerializationOptions()
{
    ReplaceObjects = true // Default is false
};
var doc = await docApi.GetDocumentAsync<TestModel>("TestModel", "0123456789", serializationOptions: options);

We would need to add a new optional parameter of type ApiClientSerializationOptions for most "GET" methods, as they currently don't expose serialization options.

@rossmills99 What do you think?

Could you also add a unit test to validate and document the behavior? e.g. DeserializeFromStream_ShouldIgnoreDefaultValues_WhenObjectNotReused.

It also affects properties with a class type.

Here is something I put together to test:

public class TestModel
{
    /*...*/

    public List<string> ListWithDefaultValue { get; set; } = ["Value1"];
    
    public InnerTestModel ObjectWithDefaultValue = new()
    {
        InnerTestModel_PropertyToCheckIfCamelCase = "defaultValue"
    };
}
// JsonNetApiClientSerializationTest
[Fact]
public void DeserializeFromStream_ShouldNotKeepDefaultValues()
{
    byte[] jsonBytes = Encoding.UTF8.GetBytes(
        "{\"MyStringDict\": { \"key2\": \"value2\" }, \"ListProperty\": [\"Value2\"], \"ObjectWithDefaultValue\": { \"InnerTestModel_NullProperty\": \"Something\" }}");

    var stream = new MemoryStream(jsonBytes);

    var serialization = new JsonNetApiClientSerialization();

    TestModel model = serialization.DeserializeFromStream<TestModel>(stream);

    Assert.Equal("Something", model.ObjectWithDefaultValue.InnerTestModel_NullProperty);
    Assert.Null(model.ObjectWithDefaultValue.InnerTestModel_PropertyToCheckIfCamelCase);

    Assert.Single(model.ListWithDefaultValue);
    Assert.Equal("Value2", model.ListWithDefaultValue[0]);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document values in a collection with a default value is appended to the default value.

2 participants